package tt.dz.order.pojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.BusinessException;

/**
 * @Title: 订单系统资源信息,对应order_resource表
 * @author zhoutian   
 * @date 2015-11-26
 */
public class SysResource {
	//资源ID
	private String resourceId;
	//依赖的父资源的id,多个用逗号分隔
	private String parent_resource_ids;
	//资源路径
	private String url;
	//功能描述
	private String resourceDesc;
	//排序号
	private int sort;
	//创建人
	private String creator;
	//创建时间
	private Date createTime;
	//有效标识,0:无效 1:有效
	private int isValid;

	public String getResourceId() {
		return resourceId;
	}

	public void setResourceId(String resourceId) {
		this.resourceId = resourceId;
	}

	public String getParent_resource_ids() {
		return parent_resource_ids;
	}

	public void setParent_resource_ids(String parent_resource_ids) {
		this.parent_resource_ids = parent_resource_ids;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getResourceDesc() {
		return resourceDesc;
	}

	public void setResourceDesc(String resourceDesc) {
		this.resourceDesc = resourceDesc;
	}

	public int getSort() {
		return sort;
	}

	public void setSort(int sort) {
		this.sort = sort;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public int getIsValid() {
		return isValid;
	}

	public void setIsValid(int isValid) {
		this.isValid = isValid;
	}

	public SysResource() {}
	
	private static final String select_all_sql = "select resource_id as resourceId, parent_resource_ids, url, resource_desc as resourceDesc, sort, is_valid as isValid, creator, createTime from order_resource";
	
	private static final String update_all_sql = "update order_resource set resource_id = :resourceId, parent_resource_ids = :parent_resource_ids, url = :url, resource_desc = :resourceDesc, sort = :sort, is_valid = :isValid, creator = :creator, createTime = :createTime";
	
	public void save() throws BusinessException{
		if(isUrlExist(this.resourceId, this.url)){
			throw new BusinessException("资源url已存在");
		}
		String sql = "insert into order_resource(resource_id, parent_resource_ids, url, resource_desc, sort, is_valid, creator, createTime) values(:resourceId, :parent_resource_ids, :url, :resourceDesc, :sort, :isValid, :creator, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void update() throws BusinessException{
		if(isUrlExist(this.resourceId, this.url)){
			throw new BusinessException("资源url已存在");
		}
		String sql = update_all_sql + " where resource_id = :resourceId";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void delete(){
		String sql = "delete from order_resource where resource_id = :resource_id";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("resource_id", this.resourceId);
		BaseDAOUtil.delete(sql, paramMap);
	}
	
	public static boolean isUrlExist(String resource_id, String url){
		String sql = select_all_sql + " where resource_id != :resource_id and url = :url";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("resource_id", resource_id);
		paramMap.put("url", url);
		List<SysResource> list = BaseDAOUtil.queryForList(sql, paramMap, SysResource.class);
		if(list != null && list.size() > 0){
			return true;
		}
		return false;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<SysResource> getAll(){
		String sql = select_all_sql + " order by cast (resource_id as numeric) asc";
		return BaseDAOUtil.queryForList(sql, new HashMap(), SysResource.class);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<SysResource> getAllValid(){
		String sql = select_all_sql + " where is_valid = 1 order by cast (resource_id as numeric) asc";
		return BaseDAOUtil.queryForList(sql, new HashMap(), SysResource.class);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static SysResource getBy(String resourceId){
		String sql = select_all_sql + " where resource_id = :resourceId";
		Map paramMap = new HashMap();
		paramMap.put("resourceId", resourceId);
		return BaseDAOUtil.queryForObject(sql, paramMap, SysResource.class);
	}
	
	public static Integer getMaxSeqNo(){
		String sql = "select resource_id from order_resource order by cast (resource_id as numeric) desc";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<Map<String, Object>> resourceIdList = BaseDAOUtil.queryForList(sql, paramMap);
		if(resourceIdList == null || resourceIdList.size() == 0){
			return 0;
		}
		Map<String, Object> resultMap = resourceIdList.get(0);
		return (Integer) Integer.valueOf((String)resultMap.get("resource_id"));
	}
}
